import {createRouter, createWebHistory} from 'vue-router'
import Login from '../views/Login.vue'

const routes = [
    {
        path: '/',
        name: 'Login',
        component: Login
    },
    {
        path: '/main',
        name: 'WebMain',
        component: () => import('@/components/WebMain.vue'),
        children: [
            {
                path: '/home',
                name: 'Home',
                component: () => import('@/views/Home.vue')
            },
            {
                path: '/favorite',
                name: 'Favorite',
                component: () => import('../views/app/favorite/Favorite.vue')
            },
            {
                path: '/resource',
                name: 'Resource',
                component: () => import('../views/system/resource/Resource.vue')
            },
            {
                path: '/role',
                name: 'Role',
                component: () => import('../views/system/role/Role.vue')
            },
            {
                path: '/account',
                name: 'Account',
                component: () => import('../views/system/account/Account.vue')
            }
        ]
    },


]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

//进入页面之前判断用户是否登录
router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
        if (sessionStorage.getItem('Authorization')) {  // 获取用户登录信息
            next();
        } else {
            next({
                path: '/',
                query: {redirect: from.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
            })
        }
    } else {
        next();
    }
})

export default router
